package com.itheima.demo6;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread1 extends Thread {
    //synchronized:进程锁   锁对象必须唯一
    public MyThread1() {
    }

    public MyThread1(String name) {
        super(name);
    }

    static int ticket = 0 ;

    static Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (ticket == 100){
                    break;
                }else {

                        Thread.sleep(10);

                    ticket++;
                    System.out.println(getName()+"正在卖第"+ticket+"张票！");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }


}
